<?php
/**
 * 获取数据(以下处理数据都已非合单为单位)
 *
 * @category
 * @package
 * @author chenping<chenping@shopex.cn>
 * @version $Id: Z
 */
class ome_event_trigger_shop_data_delivery_common
{
    protected $__deliverys = array();

    protected $__delivery_orders = array();

    protected $__sdf = array();

    final public function init($deliverys,$delivery_orders)
    {
        // 发货单
        foreach ($deliverys as $key => $value) {
            $this->__deliverys[$value['delivery_id']] = $value;
        }

        // 发货单对应订单
        $this->__delivery_orders = $delivery_orders;

        $this->__sdf = array();

        return $this;
    }


    /**
     * 不支持拆单回写
     *
     * @return void
     * @author
     **/
    protected function _nonsupport_mode_request($delivery_id)
    {
        $delivery = $this->__deliverys[$delivery_id];
        $order    = $this->__delivery_orders[$delivery_id];

        // 订单拆单判断
        $is_split = $this->_is_split_order($delivery_id);

        if ($is_split) {
            $order_delivery = $this->_get_order_delivery_ids($delivery_id);

                $check_delivery_id = $delivery['parent_id'] > 0 ? $delivery['parent_id'] : $delivery_id;

                // 如果只有一张发货成功的单子
                $tmp_delivery = array();
                foreach ($order_delivery as $key => $value) {
                    if ($value['status'] == 'succ') $tmp_delivery[strval($value['delivery_id'])] = $value['delivery_time'];
                }
                asort($tmp_delivery);
                reset($tmp_delivery); $firstDeliveryId = key($tmp_delivery);
                end($tmp_delivery);   $lastDeliveryId = key($tmp_delivery);
            $split_type = app::get('ome')->getConf('ome.order.split_type');
            if ($split_type == '1' && $order['shop_type']!='vop' && $check_delivery_id != $firstDeliveryId) { // 以第一张回写
                return false;
            }
            if ($order['shop_type']=='vop' && $split_type == '1' &&  $order['ship_status']!='1'){
                return false;
            }
            if ($split_type  == '2' && ($order['ship_status']!='1' || $check_delivery_id != $lastDeliveryId || $order['sync'] == 'succ' )) { // 以最后回写
                return false;
            }
        }

        return true;
    }

    /**
     * @param Int $delivery_id 如果是合并发货单，取的是子单
     *
     * @return void
     * @author
     **/
    public function get_sdf($delivery_id)
    {
        $delivery = $this->__deliverys[$delivery_id];
        $order    = $this->__delivery_orders[$delivery_id];
        if (!$delivery || !$order || $order['createway']!='matrix') {return array();}
        // 开启拆单
        $shop_detail = app::get('ome')->model('shop')->dump(array('shop_id'=>$delivery['shop_id']),'tbbusiness_type');

        $switch = $this->_is_split_switch($delivery_id);
        if ($switch != '0' && (!in_array($order['shop_type'],array('yunji','weidian','ecos.b2c','ecos.dzg','taobao','shopex_b2b','shopex_fy','wdwd','beibei')) && ($order['shop_type'] == 'alibaba' && $shop_detail['tbbusiness_type']!='lst') ) ){
            $isRequest = $this->_nonsupport_mode_request($delivery_id);
            if ($isRequest === false) return array();
        }

        // 获取发货单信息
        $this->_get_delivery_sdf($delivery_id);
        return $this->__sdf;
    }

    /**
     * 获取添加发货单SDF
     *
     * @return void
     * @author
     **/
    public function get_add_delivery_sdf($delivery_id){return array();}

    /**
     * 获取发货单
     *
     * @return void
     * @author
     **/
    final protected function _get_delivery_sdf($delivery_id)
    {
        $delivery = $this->__deliverys[$delivery_id];
        $order    = $this->__delivery_orders[$delivery_id];
        $corp = $this->_get_corp($delivery['logi_id']);
        $this->__sdf['delivery_id']          = $delivery['delivery_id'];
        $this->__sdf['delivery_bn']          = $delivery['delivery_bn'];
        $this->__sdf['status']               = $delivery['status'];
        $this->__sdf['logi_id']              = $delivery['logi_id'];
        $this->__sdf['logi_no']              = $delivery['logi_no'];
        $this->__sdf['crossborder_res_id']   = $corp['crossborder_res_id'];#跨境配送资源id
        $this->__sdf['corp_type']            = $corp['corp_type'];#类型(本地、跨境)
        $this->__sdf['crossborder_region_id']  = $corp['crossborder_region_id'];#发货地区域id
        $this->__sdf['logi_name']            = $corp['name'];
        $this->__sdf['logi_type']            = $corp['type'];
        $this->__sdf['is_cod']               = $delivery['is_cod'];
        $this->__sdf['itemNum']              = $delivery['itemNum'];
        $this->__sdf['delivery_time']        = $delivery['delivery_time'];
        $this->__sdf['last_modified']        = $delivery['last_modified'];
        $this->__sdf['delivery_cost_actual'] = $delivery['delivery_cost_actual'];
        $this->__sdf['create_time']          = $delivery['create_time'];
        $this->__sdf['is_protect']           = $delivery['is_protect'];
        $this->__sdf['delivery']             = $delivery['delivery'];
        $this->__sdf['memo']                 = $delivery['memo'];
        $this->__sdf['consignee'] = array(
            'name'      => $delivery['ship_name'],
            'area'      => $delivery['ship_area'],
            'addr'      => $delivery['ship_addr'],
            'zip'       => $delivery['ship_zip'],
            'email'     => $delivery['ship_email'],
            'mobile'    => $delivery['ship_mobile'],
            'telephone' => $delivery['ship_tel'],
        );

        $this->__sdf['orderinfo']['order_id']      = $order['order_id'];
        $this->__sdf['orderinfo']['order_bn']      = $order['order_bn'];
        $this->__sdf['orderinfo']['ship_status']   = $order['ship_status'];
        $this->__sdf['orderinfo']['createway']     = $order['createway'];
        $this->__sdf['orderinfo']['sync']          = $order['sync'];
        $this->__sdf['orderinfo']['is_cod']        = $order['is_cod'];
        $this->__sdf['orderinfo']['self_delivery'] = $order['self_delivery'];
        $this->__sdf['orderinfo']['order_bool_type'] = $order['order_bool_type'];
        if ($delivery['type'] == 'reject') {
            $this->__sdf['logi_no']   = $order['order_bn'];
            $this->__sdf['logi_name'] = '其他物流公司';
            $this->__sdf['logi_type'] = 'OTHER';
        }
    }

    final protected function _get_delivery_items_sdf($delivery_id)
    {
        $delivery_items = array();

        $delivery_items_detail = $this->_get_delivery_items_detail($delivery_id);
        $order_objects         = $this->_get_order_objects($delivery_id);

        foreach ($delivery_items_detail as $key => $value) {
            $order_item = $order_objects[$value['order_obj_id']]['order_items'][$value['order_item_id']];

            if ($value['item_type'] == 'pkg') {
                $number = $order_objects[$value['order_obj_id']]['quantity']*$value['number']/$order_item['nums'];

                $delivery_items['obj_'.$value['order_obj_id']] = array(
                    'name'          => trim($order_objects[$value['order_obj_id']]['name']),
                    'bn'            => trim($order_objects[$value['order_obj_id']]['bn']),
                    'number'        => $number,
                    'item_type'     => $value['item_type'],
                    'shop_goods_id' => $order_objects[$value['order_obj_id']]['shop_goods_id'],
                    'oid'           => $order_objects[$value['order_obj_id']]['oid'],
                );

            } else {
                $nums = $order_objects[$value['order_obj_id']]['quantity']*$value['number']/$order_item['nums'];
                $delivery_items['item_'.$value['order_item_id']] = array(
                    'name'          => trim($order_objects[$value['order_obj_id']]['name']),
                    'bn'            => trim($value['bn']),
                    'number'        => $value['number'],
                    'item_type'     => $value['item_type'],
                    'shop_goods_id' => $order_item['shop_goods_id'],
                    'shop_product_id' => $order_item['shop_product_id'],
                    'promotion_id'  => $order_item['promotion_id'],
                    'shop_goods_bn' =>  $order_objects[$value['order_obj_id']]['bn'],
                    'nums'          =>  $nums,
                    'oid'           => $order_objects[$value['order_obj_id']]['oid'],
                );
            }
        }

        $this->__sdf['delivery_items'] = $delivery_items;
    }

    final protected function _get_order_objects_sdf($delivery_id)
    {
        $order_objects = $this->_get_order_objects($delivery_id);

        $order_objects_sdf = array();
        foreach ($order_objects as $object) {

            $order_items_sdf = array();
            foreach ($object['order_items'] as $item) {
                $order_items_sdf[$item['item_id']]['bn']            = $item['bn'];
                $order_items_sdf[$item['item_id']]['shop_goods_id'] = $item['shop_goods_id'];
                $order_items_sdf[$item['item_id']]['sendnum']       = $item['sendnum'];
                $order_items_sdf[$item['item_id']]['name']          = $item['name'];
                $order_items_sdf[$item['item_id']]['promotion_id']  = $item['promotion_id'];
                $order_items_sdf[$item['item_id']]['item_type']     = $item['item_type'];
                $order_items_sdf[$item['item_id']]['nums']          = $item['nums'];
            }

            $order_objects_sdf[$object['obj_id']]['bn']            = $object['bn'];
            $order_objects_sdf[$object['obj_id']]['oid']           = $object['oid'];
            $order_objects_sdf[$object['obj_id']]['shop_goods_id'] = $object['shop_goods_id'];
            $order_objects_sdf[$object['obj_id']]['quantity']      = $object['quantity'];
            $order_objects_sdf[$object['obj_id']]['name']          = $object['name'];
            $order_objects_sdf[$object['obj_id']]['obj_type']      = $object['obj_type'];
            $order_objects_sdf[$object['obj_id']]['order_items']   = $order_items_sdf;

        }

        $this->__sdf['orderinfo']['order_objects'] = $order_objects_sdf;
    }
    #获取所有订单明细(包括已删除商品)
    final protected function _get_order_all_objects_sdf($delivery_id){
       $order_objects = $this->_get_all_order_objects($delivery_id);

       $order_objects_sdf = array();
       foreach ($order_objects as $object) {
          $order_items_sdf = array();
          foreach ($object['order_items'] as $item) {
             $order_items_sdf[$item['item_id']]['bn']            = $item['bn'];
             $order_items_sdf[$item['item_id']]['shop_goods_id'] = $item['shop_goods_id'];
             $order_items_sdf[$item['item_id']]['sendnum']       = $item['sendnum'];
             $order_items_sdf[$item['item_id']]['name']          = $item['name'];
             $order_items_sdf[$item['item_id']]['promotion_id']  = $item['promotion_id'];
             $order_items_sdf[$item['item_id']]['item_type']     = $item['item_type'];
             $order_items_sdf[$item['item_id']]['nums']          = $item['nums'];
          }
          $order_objects_sdf[$object['obj_id']]['bn']            = $object['bn'];
          $order_objects_sdf[$object['obj_id']]['oid']           = $object['oid'];
          $order_objects_sdf[$object['obj_id']]['shop_goods_id'] = $object['shop_goods_id'];
          $order_objects_sdf[$object['obj_id']]['quantity']      = $object['quantity'];
          $order_objects_sdf[$object['obj_id']]['name']          = $object['name'];
          $order_objects_sdf[$object['obj_id']]['obj_type']      = $object['obj_type'];
          $order_objects_sdf[$object['obj_id']]['order_items']   = $order_items_sdf;
       }
       $this->__sdf['orderinfo']['order_objects'] = $order_objects_sdf;
    }
    final protected function _get_all_order_objects($delivery_id){
       static $order_all_objects;
       $order = $this->__delivery_orders[$delivery_id];

       if (isset($order_all_objects[$order['order_id']])) return $order_all_objects[$order['order_id']];

       $orderIds = array();
       foreach ($this->__delivery_orders as $key => $value) {
          $orderIds[] = $value['order_id'];
          $order_all_objects[$value['order_id']] = array();
       }

       $orderItemModel = app::get('ome')->model('order_items');
       $rows = $orderItemModel->getList('*',array('order_id'=>$orderIds));
       $order_items = array();
       foreach ($rows as $row) {
          $order_items[$row['obj_id']][$row['item_id']] = $row;
       }

       $orderObjModel = app::get('ome')->model('order_objects');
       $rows = $orderObjModel->getList('*',array('obj_id'=>array_keys($order_items)));
       foreach ($rows as $row) {
          $row['order_items'] = $order_items[$row['obj_id']];
          $order_all_objects[$row['order_id']][$row['obj_id']] = $row;
       }
       return $order_all_objects[$order['order_id']];
    }

    final protected function _get_members_sdf($delivery_id)
    {
        $member = $this->_get_members($delivery_id);

        $this->__sdf['memberinfo']['uname'] = $member['uname'];
    }

    final protected function _get_product_serial_sdf($delivery_id)
    {
        $serial = $this->_get_product_serial($delivery_id);
        if (!$serial) return;

        $feature = array();

        $delivery = $this->__deliverys[$delivery_id];
        if ($delivery['parent_id'] > 0) {
            // 获取父发货单对应的所有订单
            $deliveryItemDetailModel = app::get('ome')->model('delivery_items_detail');
            $deliveryItemDetailList = $deliveryItemDetailModel->getList('*',array('delivery_id'=>$delivery['parent_id']));

            $product_serial = array();
            foreach ($deliveryItemDetailList as $key => $value) {
                if (!$serial[$value['product_id']]) continue;

                $product_serial[$value['order_item_id']] = array_splice($serial[$value['product_id']], 0, $value['number']);
            }


        } else {
            $deliveryItemDetailList = $this->_get_delivery_items_detail($delivery_id);
            $product_serial = array();
            foreach ($deliveryItemDetailList as $key => $value) {
                if (!$serial[$value['product_id']]) continue;

                $product_serial[$value['order_item_id']] = array_splice($serial[$value['product_id']], 0, $value['number']);

            }
        }

        $order_objects = $this->_get_order_objects($delivery_id);
        foreach ($order_objects as $object) {
            $obj_product_serial = array();
            foreach ($object['order_items'] as $item) {
                if ($product_serial[$item['item_id']])
                    $obj_product_serial[] = implode(',',$product_serial[$item['item_id']]);
            }

            if ($object['oid'] && $obj_product_serial) {
                $feature[] = $object['oid'].':'.implode('|',$obj_product_serial);
            }
        }

        if ($feature) $this->__sdf['feature'] = 'identCode='.implode('|',$feature);
    }

    /**
     * 拆单
     *
     * @return void
     * @author
     **/
    final protected function _get_split_sdf($delivery_id)
    {
        $delivery = $this->__deliverys[$delivery_id];
        $order    = $this->__delivery_orders[$delivery_id];
        if ($delivery['type'] != 'normal') return;

        // 开启了拆单
        $switch = $this->_is_split_switch($delivery_id);
        if ($switch == '0') return ;

        // 判断订单是否拆单
        $is_split = $this->_is_split_order($delivery_id);
        if (!$is_split) return ;


        $delivery_items_detail = $this->_get_delivery_items_detail($delivery_id);
        $order_objects = $this->_get_order_objects($delivery_id);


        // 如果有删除明细，全量回写(oid不存在，肯定有删除)
        $delete_exist = false;
        foreach ($order_objects as $object) {
            if (!$object['oid'] && $object['shop_goods_id'] != '-1') {
                $delete_exist = true; break;
            }
        }

        if ($delete_exist) return;
        $this->__sdf['is_split'] = 1;
        // 子单号
        $order_oid = array();
        $order_shop_goods_id = array();
        foreach ($order_objects as $object) {
            $order_oid[$object['obj_id']] = $object['oid'];
            if ($object['shop_goods_id'] && $object['shop_goods_id'] != '-1') {
                $order_shop_goods_id[$object['obj_id']] = $object['shop_goods_id'];
                
            }
        }


        // 如果有退款,默认只查询15条有商品明细的退款记录
        $refund_bn = array();
        $refundApplyModel = app::get('ome')->model('refund_apply');
        $refundApplyList = $refundApplyModel->getList('product_data', array('order_id'=>$order['order_id'], 'status'=>'4', 'product_data|noequal'=>''), 0, 15);
        if ($refundApplyList) {
            foreach ($refundApplyList as $key => $value) {
                $product_data = unserialize($value['product_data']);
                foreach ($product_data as $k => $v) {
                    $refund_bn[]    = $v['bn'];
                }
            }
        }

        // 如果有余单撤销,把其他未返回的一并回传了

        // 正常回写
        $oid_list = array();
        $oid_numlist = array();
        $product_list = array();
        foreach ($delivery_items_detail as $key => $value) {
            if (in_array($value['bn'], $refund_bn)) continue;

            $oid_list[] = $order_oid[$value['order_obj_id']];
            $oid_numlist[] = array('oid'=>$order_oid[$value['order_obj_id']],'nums'=>$value['number']);
            $product_list[] = array('barcode'=>$order_shop_goods_id[$value['order_obj_id']],'amount'=>$value['number']);
        }
        $this->__sdf['oid_numlist'] = $oid_numlist;
        $this->__sdf['oid_list'] = array_unique(array_filter($oid_list));
        $this->__sdf['product_list'] = $product_list;
        return ;
    }

    /**
     * 获取分销王格式的拆分数据
     *
     * @return void
     * @author
     **/
    final protected function _get_fxwfat_split_sdf($delivery_id)
    {
        $delivery = $this->__deliverys[$delivery_id];
        $order    = $this->__delivery_orders[$delivery_id];

        if ($delivery['type'] != 'normal' || !in_array($order['shop_type'], array('shopex_b2b','ecshop'))) return;

        // 开启了拆单
        $switch = $this->_is_split_switch($delivery_id);
        if ($switch == '0') return ;

        // 判断订单是否拆单
        $is_split = $this->_is_split_order($delivery_id);
        if (!$is_split) return ;


        $delivery_items_detail = $this->_get_delivery_items_detail($delivery_id);
        $order_objects = $this->_get_order_objects($delivery_id);

        $this->__sdf['is_split'] = 1;

        // 正常回写
        $delivery_items = array();
        foreach ($delivery_items_detail as $key => $value) {
            if ($order_objects[$value['order_obj_id']]['order_items'][$value['order_item_id']]['nums'] == $order_objects[$value['order_obj_id']]['order_items'][$value['order_item_id']]['sendnum'] || $order_objects[$value['order_obj_id']]['order_items'][$value['order_item_id']]['nums'] == $value['number']){

                if ($value['item_type'] == 'pkg') {
                    $number = $order_objects[$value['order_obj_id']]['quantity'];

                    $delivery_items['obj_'.$value['order_obj_id']] = array(
                        'name'          => trim($order_objects[$value['order_obj_id']]['name']),
                        'bn'            => trim($order_objects[$value['order_obj_id']]['bn']),
                        'number'        => $number,
                        'item_type'     => $value['item_type'],
                        'shop_goods_id' => $order_objects[$value['order_obj_id']]['shop_goods_id'],
                    );

                } else {
                    $delivery_items['item_'.$value['order_item_id']] = array(
                        'name'          => trim($order_objects[$value['order_obj_id']]['name']),
                        'bn'            => trim($value['bn']),
                        'number'        => $order_objects[$value['order_obj_id']]['order_items'][$value['order_item_id']]['nums'],
                        'item_type'     => $value['item_type'],
                        'shop_goods_id' => $order_item['shop_goods_id'],
                        'promotion_id'  => $order_item['promotion_id'],
                    );
                }
            }
        }

        $this->__sdf['delivery_items'] = $delivery_items;

        return ;
    }


    /**
     * 是否开启拆单功能
     *
     * @return void
     * @author
     **/
    final protected function _is_split_switch($delivery_id)
    {

        if (app::get('ome')->getConf('ome.order.split') != '1') {
            return '0';
        }

        // 用发货单和订单购买数量匹配是否拆单
        $delivery_items_detail = $this->_get_delivery_items_detail($delivery_id);
        $order_objects = $this->_get_order_objects($delivery_id);

        $oid = true;
        foreach ($delivery_items_detail as $detail) {
            if ($detail['number'] != $order_objects[$detail['order_obj_id']]['order_items'][$detail['order_item_id']]['nums']) {
                $oid = false;break;
            }
        }

        return $oid===true ? '1' : '2';
    }

    /**
     * 判断是否拆单
     *
     * @return void
     * @author
     **/
    final protected function _is_split_order($delivery_id)
    {
        $order = $this->__delivery_orders[$delivery_id];

        // 如果存在未发货订单
        if ($order['ship_status'] == '2' || $order['process_status'] == 'remain_cancel' || $order['process_status'] == 'splitting') {
            return true;
        }

        $order_delivery = $this->_get_order_delivery_ids($delivery_id);
        if (count($order_delivery) > 1) {
            return true;
        }

        return false;
    }

    /**
     * 获取订单的所有发货单ID
     *
     * @return void
     * @author
     **/
    final protected function _get_order_delivery_ids($delivery_id)
    {
        static $order_delivery;

        $order = $this->__delivery_orders[$delivery_id];

        if (isset($order_delivery[$order['order_id']])) return $order_delivery[$order['order_id']];

        // 订单的发货单数
        $sql = "SELECT d.delivery_id,d.status,d.delivery_time
                FROM sdb_ome_delivery_order AS do
                LEFT JOIN sdb_ome_delivery AS d
                ON(do.delivery_id=d.delivery_id)
                WHERE do.order_id='".$order['order_id']."' AND d.parent_id='0' AND d.disabled='false' AND d.status IN('succ','progress','ready')";
        $rows = kernel::database()->select($sql);

        $order_delivery[$order['order_id']] = array();

        if ($rows){
            foreach ($rows as $row) {
                $order_delivery[$order['order_id']][$row['delivery_id']] = $row;
            }
        }

        return $order_delivery[$order['order_id']];
    }

    protected function _get_order_deliverys_items_detail($delivery_id)
    {
        static $detail;

        if (isset($detail[$delivery_id])) return $detail[$delivery_id];

        $detail[$delivery_id] = array();

        $delivery_ids = array();
        foreach ((array) $this->_get_order_delivery_ids($delivery_id) as $value) {
            $delivery_ids[] = array_keys($value);
        }

        if ($delivery_ids) {
            $deliveryItemDetailModel = app::get('ome')->model('delivery_items_detail');
            $deliveryItemDetailList = $deliveryItemDetailModel->getList('*',array('delivery_id'=>$delivery_ids));

            foreach ($deliveryItemDetailList as $key => $value) {
                $detail[$value['delivery_id']][] = $value;
            }
        }

        return $detail[$delivery_id];
    }

    final protected function _get_corp($corp_id)
    {
        static $corps;

     //   if (isset($corps[$corp_id])) return $corps[$corp_id];

        $corpIds = array();
        foreach ($this->__deliverys as $delivery) {
            $corpIds[] = $delivery['logi_id'];

            $corps[$delivery['logi_id']] = array();
        }
        $corpModel = app::get('ome')->model('dly_corp');
    //    $rows = $corpModel->getList('corp_id,corp_type,name',array('corp_id'=>$corpIds));
        $rows = $corpModel->db->selectrow("select type,name from sdb_ome_dly_corp where corp_id=".$corp_id);

      return $rows;
    }

    final protected function _get_order_extend($delivery_id)
    {
        static $order_extends;

        $order = $this->__delivery_orders[$delivery_id];

        if (isset($order_extends[$order['order_id']])) return $order_extends[$order['order_id']];

        $orderIds = array();
        foreach ($this->__delivery_orders as $key => $value) {
            $orderIds[] = $value['order_id'];

            $order_extends[$value['order_id']] = array();
        }

        $orderExtendModel = app::get('ome')->model('order_extend');
        $rows = $orderExtendModel->getList('*',array('order_id'=>$orderIds));
        foreach ($rows as $row) {
            $order_extends[$row['order_id']] = $row;
        }

        return $order_extends[$order['order_id']];
    }

    final protected function _get_order_objects($delivery_id)
    {
        static $order_objects;

        $order = $this->__delivery_orders[$delivery_id];

        if (isset($order_objects[$order['order_id']])) return $order_objects[$order['order_id']];

        $orderIds = array();
        foreach ($this->__delivery_orders as $key => $value) {
            $orderIds[] = $value['order_id'];

            $order_objects[$value['order_id']] = array();
        }

        $orderItemModel = app::get('ome')->model('order_items');
        $rows = $orderItemModel->getList('*',array('order_id'=>$orderIds,'delete'=>'false'));
        $order_items = array();
        foreach ($rows as $row) {
            $order_items[$row['obj_id']][$row['item_id']] = $row;
        }

        $orderObjModel = app::get('ome')->model('order_objects');
        $rows = $orderObjModel->getList('*',array('obj_id'=>array_keys($order_items)));
        foreach ($rows as $row) {
            $row['order_items'] = $order_items[$row['obj_id']];
            $order_objects[$row['order_id']][$row['obj_id']] = $row;
        }

        return $order_objects[$order['order_id']];
    }

    final protected function _get_delivery_items_detail($delivery_id)
    {
        static $delivery_items_detail;

        if (isset($delivery_items_detail[$delivery_id])) return $delivery_items_detail[$delivery_id];

        $deliveryIds = array();
        foreach ($this->__deliverys as $delivery) {
            $deliveryIds[] = $delivery['delivery_id'];

            $delivery_items_detail[$delivery['delivery_id']] = array();
        }

        $deliveryItemDetailModel = app::get('ome')->model('delivery_items_detail');
        $deliveryItemDetailList = $deliveryItemDetailModel->getList('*',array('delivery_id'=>$deliveryIds));

        foreach ($deliveryItemDetailList as $key => $value) {
            $delivery_items_detail[$value['delivery_id']][] = $value;
        }

        return $delivery_items_detail[$delivery_id];
    }

    final protected function _get_product_serial($delivery_id)
    {
        static $product_serial;
        $delivery = $this->__deliverys[$delivery_id];

        $deliveryIds = $deliveryBns= array();
        foreach ($this->__deliverys as $d) {
            $deliveryIds[] = $d['delivery_id'];
            $deliveryBns[] = $d['delivery_bn'];
            if ($d['parent_id'] > 0) {
                $deliveryIds[] = $d['parent_id'];
            }
        }

        $dlyItemsSerialObj    = app::get('ome')->model('delivery_items_serial');
        $rows = $dlyItemsSerialObj->getList('product_id,serial_number', array('delivery_id'=>$deliveryIds), 0, -1);
        foreach ($rows as $row) {
            $product_serial[$row['product_id']][] = $row['serial_number'];
        }

        return $product_serial;
    }

    final protected function _get_members($delivery_id)
    {
        static $members;

        $order = $this->__delivery_orders[$delivery_id];

        if (isset($members[$order['member_id']])) return $members[$order['member_id']];

        $memberIds = array();
        foreach ($this->__delivery_orders as $key => $value) {
            $memberIds[] = $value['member_id'];

            $members[$value['member_id']] = array();
        }

        if (!$members) return array();

        $memberModel = app::get('ome')->model('members');
        $rows = $memberModel->getList('*',array('member_id'=>$memberIds));
        foreach ($rows as $row) {
            $members[$row['member_id']] = $row;
        }

        return $members[$order['member_id']];
    }
}

